const { saveGaojings, getGaojings,getTerminalGaojing } = require("../services/gaojing.service.js");
const { saveLinks, getLinks } = require("../services/links.service.js");
const { getGaojing } = require("../common/common.js");
const { getOptical } = require("../services/opticals.service.js");
const err = require("../constant/err.type.js");
const Gaojings = require("../model/gaojing.model.js");

class GaojingController {
    async saveGaojing(ctx, next) {
        var res;
        try {
            const { ...gaojing } = ctx.request.body;
            var { linkId, gaojingType, linkName, status, statusValue,gaojingType } = gaojing;
            let ls;
            let ps;
            if(gaojingType == 0){
                //是模拟告警，查一下是否有未解除的模拟告警，如果有，则禁止模拟告警。
                let temp = await getTerminalGaojing({linkName});
                if(temp instanceof Object && Object.keys(temp).length > 0){
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: `${linkName}上有未解除的告警，无法再次模拟，请先解除告警后再试！`, data: "" }), ctx);
                return;
                }
            }
            ls = await saveLinks({ linkId, status, statusValue });
            /* if (!ls) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "保存线路信息失败", data: "" }), ctx);
                return;
            } */
            ps = await getOptical({ linkName });
            if (!ps || !(ps instanceof Array) || ps.length == 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取坐标信息错误", data: "" }), ctx);
                return;
            }
            if (gaojing.gaojingId > 0) {

            } else {
                let gj = await getGaojing(ps, statusValue);
                if (gj.code != 0) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "设置告警信息失败", data: "" }), ctx);
                    return;
                }
                gaojing.originId = gj.data.originId;
                gaojing.origin = gj.data.origin;
                gaojing.finishing = gj.data.finishing;
                gaojing.finishingId = gj.data.finishingId;
                gaojing.distance = gj.data.distance;
                gaojing.X = gj.data.x;
                gaojing.Y = gj.data.y;
            }
            res = await saveGaojings(gaojing);
            if (!res) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "保存告警失败.", data: "" }), ctx);
                return;
            } else {
                ctx.status = 200;
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "保存告警成功", data: { gaojing: res, list: ps } }), ctx);
                return;
            }
        } catch (error) {
            console.log(error);
            ctx.app.emit(
                "error",
                err.setDefaultValue({
                    code: -1,
                    msg: "保存告警时出现错误，代码为" + error,
                    data: "",
                }),
                ctx
            );
            return;
        }
    }
    async getGaojingById(ctx, next) {
        var { gaojingId } = ctx.request.body;
        if (gaojingId > 0) {
            let whereOpt = { gaojingId };
            let res = await getGaojings(whereOpt);
            if (res && res instanceof Object && Object.keys(res).length > 0) {
                var { gaojingType, linkId, linkName, status, statusValue } = res;
                if (res.times > 0) {
                    let tm = new Date(res.times * 1000);
                    res.times = `${tm.toLocaleDateString().replace(/\//g, "-")} ${tm.toTimeString().substr(0, 8)}`;
                }
                if (res.endtimes > 0) {
                    let tm = new Date(res.endtimes * 1000);
                    res.endtimes = `${tm.toLocaleDateString().replace(/\//g, "-")} ${tm.toTimeString().substr(0, 8)}`;
                }
                let list = await getOptical({ linkName });
                if (!list || !(list instanceof Array) || list.length == 0) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取光缆信息失败", data: res }), ctx);
                    return;
                }
                //let data = getGaojing(list, statusValue);
                let link = await getLinks({ linkId });
                if (!link || !(link instanceof Array) || link.length == 0) {
                    link = {}
                }else{
                    link = link[0]
                }
                var dt = Object.assign({}, link, res/* , data.data */)
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "ok", data: dt }), ctx);
                return;

            } else {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取告警信息失败", data: "" }), ctx);
                return;
            }

        } else {
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取告警ID失败", data: "" }), ctx);
            return;
        }
    }
    async getGaojingList(ctx, next) {
        var res;
        try {
            var { gaojingId, linkId, linkName } = ctx.request.body;
            var whereOpt = {};
            gaojingId > 0 && Object.assign(whereOpt, { gaojingId });
            linkId > 0 && Object.assign(whereOpt, { linkId });
            linkName && Object.assign(whereOpt, { linkName });
            res = await getGaojings(whereOpt);
            if (!res) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取告警失败", data: "" }), ctx);
                return;
            } else {
                ctx.status = 200;
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "OK", data: res }), ctx);
                return;
            }
        } catch (error) {
            console.log(error);
            ctx.app.emit(
                "error",
                err.setDefaultValue({
                    code: -1,
                    msg: "查询告警时出现错误，代码为" + error,
                    data: "",
                }),
                ctx
            );
            return;
        }
    }
}

module.exports = new GaojingController();